package com.qf.service.impl;

import com.qf.dao.CartDao;
import com.qf.entity.Cart;
import com.qf.entity.Goods;
import com.qf.service.CartService;
import com.qf.service.GoodsService;
import com.qf.utils.MyBatisUtils;
import org.apache.taglibs.standard.tag.common.xml.ParseSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author wgy
 * @version V1.0
 * @project gp12_xiaomi
 * @package com.qf.service.impl
 * @description: ${TODO}
 * @date 2022/6/23 16:37
 */
@Service
@Transactional
public class CartServiceImpl implements CartService {

    private GoodsService goodsService;

    private CartDao cartDao;

    @Autowired
    public void setGoodsService(GoodsService goodsService) {
        this.goodsService = goodsService;
    }

    @Autowired
    public void setCartDao(CartDao cartDao) {
        this.cartDao = cartDao;
    }

    @Override
    public void add(Cart cart) {
        cartDao.insert(cart);
    }

    @Override
    public Cart findByUidAndPid(Integer id, int pid) {
        return cartDao.selectByUidAndPid(id,pid);
    }

    @Override
    public void update(Cart cart) {
        cartDao.update(cart);

    }

    @Override
    public List<Cart> findByUid(Integer id) {
        List<Cart> cartList = cartDao.selectByUid(id);
        if (cartList != null) {
            //根据商品id查询商品
            for (Cart cart : cartList) {
                Goods goods=goodsService.findById(cart.getPid());
                cart.setGoods(goods);
            }
        }
        return cartList;
    }

    @Override
    public void delete(Integer uid, int pid) {
        cartDao.delete(uid,pid);

    }

    @Override
    public void deleteByUid(Integer uid) {
        cartDao.deleteByUid(uid);

    }
}
